package cloud

import (
	"dgo/MyProj/PrjHttpUtils"
	"dgo/goutils/utils"
	"dgo/plg/plgnet"
	"net/http"
	"sync/atomic"
	"time"
)

func (this *PlgVxApi) onHttpVxTemplSendMessage(w http.ResponseWriter, r *http.Request) {
	PrjHttpUtils.AllowCrossAccessSet(w)
	req := plgnet.NewPlgHttpRec(w, r)
	req.DecodeRequestBodyAsJSON()

	var sb utils.BytesBuilder
	sb.Appendf("[%s]请求参数\n", req.ClientAddr())
	sb.Appendf("url:%s\n", req.ReqR.URL.String())
	sb.Appendf("Method:%s\n", req.ReqR.Method)
	PrjHttpUtils.GetRequestHeader(req.ReqR.Header, ":", "\n", &sb)
	sb.Appendf("\n")
	sb.Write(req.BodyBuf)
	sb.Appendf("\n")
	t1 := time.Now()
	req.OnResponseFunc = func(respHeader http.Header, body []byte) {
		sb.Appendf("响应(%d ms)\n", time.Since(t1).Milliseconds())
		PrjHttpUtils.GetRequestHeader(respHeader, ":", "\n", &sb)
		sb.Appendf("\n")
		sb.Write(body)
		sb.Appendf("\n")
		this.msglst.LogMsg("%s", sb.String())
	}

	defer req.Close()
	atomic.AddInt32(&this.templmsg_cnt, 1)
	if this.templmsg_shutdown == 0 {
		err := this.VxUtil.SendTemplMessage(req.Req)
		if err != nil {
			req.ResponseJSONValues("code", -1, "msg", err.Error())
			return
		}
		req.ResponseJSONValues("code", 0)
	} else {
		req.ResponseJSONValues("code", 0, "msg", "templmsg_shutdown(1)忽略发送")
	}

}
